(function() {
    'use strict';

    angular
        .module('zhserverApp')
        .controller('RuleRepositoryCreateController', RuleRepositoryCreateController);

    RuleRepositoryCreateController.$inject = ['$scope','BingMing','AlertService','entity', 'RuleRepository','$state','$resource','Account'];

    function RuleRepositoryCreateController ($scope,BingMing,AlertService,entity, RuleRepository,$state,$resource,Account) {
        var vm = this;
        $scope.sa=entity;
        $scope.zxmvar="";
        $scope.jtzxmvar="";
        vm.ruleRepository = entity;
        vm.clear = clear;
        vm.save = save;
        //征行名
        vm.zxName=null;
        //病名
        vm.bName=null;
        //正像
        vm.z_x=null;

        //编辑时；
        vm.edit_bmName=null;
        vm.edit_zxingName=null;

        //拼装
        var zxs=[];
        var bszxs=[];

        //结果
        //具体正想
        $scope.jtzx = [];
        $scope.bszzjtzx=[];

        //当前症象：
        vm. zx=[];

        var data=eval("["+vm.ruleRepository.zhengxingList+"]");
        console.log(JSON.stringify(data));
        for (var i=0;i<data.length;i++){
            var a=JSON.stringify(data[i]);
            var b=a.split('"');
            vm.zx.push(b[3]);
            if(i==data.length-1){
                vm.bszx=b[3];
            }
        }

        loadAllBM();


        function loadAllBM () {
            BingMing.query(onSuccess, onError);

            function onSuccess(data, headers) {
                vm.bName = data;
                if(vm.ruleRepository.id!=null){
                    vm.edit_bmName=data[0];
                    console.log("vm.edit_bmName:"+vm.edit_bmName+"---data[0]:"+data[0]);
                    vm.edit_bmName.bmName=vm.ruleRepository.bingmingName;
                    vm.ruleRepository.bingmingName=vm.edit_bmName;
                }
            }
            function onError(error) {
                AlertService.error(error.data.message);
                console.log(error.data.message);
            }


            Account.get(function(data){
                console.log(data.data.login);
                vm.ruleRepository.doctorName=data.data.login;
            },function(error){});


        }

        function clear () {
            $state.go("^");
        }

        $scope.$watch('vm.ruleRepository.bingmingName',function(newValue,oldValue, scope){

            if (newValue!=null && !(newValue instanceof Object)) {
                zxs=[];
                bszxs=[];
                console.log("newValue:"+newValue);
                //获取证型
                var url= "api/zheng-xings-byBM/"+newValue;
                var zx_resource=$resource(url);
                zx_resource.query(function (data) {
                    vm.zxName=data;
                }, function (error) {
                    console.log(error.data.message);
                });

                //
                //获取症象名
                var url2= "api/zheng-xings-mings-byBM/"+newValue;
                var zxm_resource=$resource(url2);
                zxm_resource.query(function (data) {
                    vm.zhengxiangmings=data;
                    if(vm.zhengxiangmings.length==0){
                        $scope.zxs=null;
                        $scope.bszxs=null;
                        zxs=[];
                        bszxs=[];
                    }
                    //获取具体正像
                    angular.forEach(data, function(data2){


                        var z_x = {
                            //症象名
                            z_x_name: "",
                            //具体症象
                            z_x_JT: []
                        };
                        //伴随症状
                        var bszz={
                            bszz_z_x_name: "",
                            //具体症象
                            bszz_z_x_JT: []
                        };

                        var z_x_name=[];
                        var z_x_JT=[];

                        //伴随症状
                        var bszz_z_x_name=[];
                        var bszz_z_x_JT=[];
                        if(data2.zxmName!="伴随症状"){
                            z_x_name.push(data2.zxmName);
                            var url3= "api/j-t-zheng-xiangs-byZXM/"+data2.zxmName+"/"+data2.bingming.bmName;
                            var jtzx_resource=$resource(url3);
                            jtzx_resource.query(function (data) {
                                angular.forEach(data, function(data2){
                                    z_x_JT.push(data2.jtzxmName);
                                });
                            }, function (error) {
                                console.log(error.data.message);
                            });
                            z_x.z_x_name=z_x_name;
                            z_x.z_x_JT=z_x_JT;
                            zxs.push(z_x);
                        }else{
                            bszz_z_x_name.push(data2.zxmName);
                            var url3= "api/j-t-zheng-xiangs-byZXM/"+data2.zxmName+"/"+data2.bingming.bmName;
                            var jtzx_resource=$resource(url3);
                            jtzx_resource.query(function (data) {

                                angular.forEach(data, function(data2){
                                    bszz_z_x_JT.push(data2.jtzxmName);
                                });

                            }, function (error) {
                                console.log(error.data.message);
                            });
                            bszz.bszz_z_x_name=bszz_z_x_name;
                            bszz.bszz_z_x_JT=bszz_z_x_JT;
                            bszxs.push(bszz);
                        }
                    });
                }, function (error) {
                    console.log(error.data.message);
                });
            }else{
                $scope.zxs=null;
                $scope.bszxs=null;
                zxs=[];
                bszxs=[];

            };

            vm.zxs=zxs;
            $scope.zxs=zxs;
            vm.bszxs=bszxs;
            $scope.bszxs=bszxs;
        });
     ;


        function save () {
            var mlist=[];
            if($scope.jtzx.length!=0&& $scope.bszzjtzx.length!=0){
                vm.ruleRepository.zhengxingList="";
                angular.forEach($scope.bszzjtzx,function (item) {
                    if(item!=null){
                        mlist.push(item);
                    }
                });
                $scope.jtzx.push(mlist);
                for(var i=0;i<$scope.jtzx.length;i++){
                    if(i+1!=$scope.jtzx.length){
                        vm.ruleRepository.zhengxingList+="{"+"'"+vm.zhengxiangmings[i].zxmName+"'"+":"+"'"+$scope.jtzx[i]+"'"+"}"+",";
                    }else{
                        vm.ruleRepository.zhengxingList+="{"+vm.zhengxiangmings[i].zxmName+":"+"'"+$scope.jtzx[i]+"'"+"}"
                    }

                }
                if(vm.ruleRepository.bingmingName instanceof Object){
                    vm.ruleRepository.bingmingName=vm.ruleRepository.bingmingName.bmName;
                }
                console.log(vm.ruleRepository);

                if (vm.ruleRepository.id !== null) {
                    RuleRepository.update(vm.ruleRepository, onSaveSuccess, onSaveError);
                    $state.go('zhRule-repositorymySuffix', null, { reload: 'zhRule-repositorymySuffix' });
                } else {
                    RuleRepository.save(vm.ruleRepository, onSaveSuccess, onSaveError);
                    $state.go('zhRule-repositorymySuffix', null, { reload: 'zhRule-repositorymySuffix' });

                }
            }else{
                console.log("失败");
            }
        }

        function onSaveSuccess (result) {
            $scope.$emit('zhserverApp:ruleRepositoryUpdate', result);
            vm.isSaving = false;
        }

        function onSaveError () {
            vm.isSaving = false;
        }
    }
    angular
        .module('zhserverApp')
        .directive('repeatFinish',function(){
        return {
            scope:false,//false代表 scope可以直接使用上面控制器中scope的变量。
            link: function(scope,element,attr){

                if(scope.$last == true){
                    var data=eval("["+scope.sa.zhengxingList+"]");
                  for (var i=0;i< $(".zxm").length;i++){
                      var a=JSON.stringify(data[i]);
                      var b=a.split('"');


                  }
                    //在此位置进行配对，然后获取doc元素，设置页面已选择

                    // for(var i=0;i<data.length;i++){
                    //
                    // }


                }
            }
        }
    })
})();
